home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / gnome-applets-data / invest / widgets.py < prev   
Encoding:
Python Source  |  2009-04-23  |  6.3 KB  |  215 lines

  1. import os, time
  2. from os.path import *
  3. import gnomeapplet, gtk, gtk.gdk, gconf, gobject, pango
  4. from gettext import gettext as _
  5. import gtk, gobject
  6. import csv, os
  7. from gettext import gettext as _
  8. import invest, invest.about, invest.chart
  9. from invest import *
  10.  
  11. COLORSCALE_POSITIVE = [
  12.     "white",
  13.     "#ad7fa8",
  14.     "#75507b",
  15.     "#5c3566",
  16.     "#729fcf",
  17.     "#3465a4",
  18.     "#204a87",
  19.     "#8ae234",
  20.     "#73d216",
  21.     "#4e9a06",
  22. ]
  23. GREEN = COLORSCALE_POSITIVE[-1]
  24. COLORSCALE_NEGATIVE = [
  25.     "white",
  26.     "#fce94f",
  27.     "#e9b96e",
  28.     "#fcaf3e",
  29.     "#c17d11",
  30.     "#f57900",
  31.     "#ce5c00",
  32.     "#ef2929",
  33.     "#cc0000",
  34.     "#a40000",
  35. ]
  36. RED = COLORSCALE_NEGATIVE[-1]
  37.  
  38. TICKER_TIMEOUT = 10000#3*60*1000#
  39.  
  40. class InvestWidget(gtk.TreeView):
  41.     def __init__(self, quotes_updater):
  42.         gtk.TreeView.__init__(self)
  43.         self.set_property("rules-hint", True)
  44.         self.set_reorderable(True)
  45.  
  46.         simple_quotes_only = quotes_updater.simple_quotes_only()
  47.  
  48.         # model: SYMBOL, TICKER_ONLY, BALANCE, BALANCE_PCT, VALUE, VARIATION_PCT, PB
  49.         # Translators: these words all refer to a stock. Last is short
  50.         # for "last price". Gain is referring to the gain since the 
  51.         # stock was purchased.
  52.         col_names = [_('Ticker'), _('Last'), _('Change %'), _('Chart'), _('Gain'), _('Gain %')]
  53.         col_cellgetdata_functions = [self._getcelldata_symbol, self._getcelldata_value,
  54.             self._getcelldata_variation, None, self._getcelldata_balance, 
  55.             self._getcelldata_balancepct]
  56.         for i, col_name in enumerate(col_names):
  57.             if i < 3:
  58.                 cell = gtk.CellRendererText()
  59.                 column = gtk.TreeViewColumn (col_name, cell)
  60.                 column.set_cell_data_func(cell, col_cellgetdata_functions[i])
  61.                 self.append_column(column)
  62.             elif i == 3:
  63.                 cell_pb = gtk.CellRendererPixbuf()
  64.                 column = gtk.TreeViewColumn (col_name, cell_pb, pixbuf=6)
  65.                 self.append_column(column)
  66.             else:
  67.                 # add the last two column only if we have any positions
  68.                 if simple_quotes_only == False:
  69.                     cell = gtk.CellRendererText()
  70.                     column = gtk.TreeViewColumn (col_name, cell)
  71.                     column.set_cell_data_func(cell, col_cellgetdata_functions[i])
  72.                     self.append_column(column)
  73.  
  74.         if simple_quotes_only == True:
  75.             self.set_property('headers-visible', False)
  76.  
  77.         self.connect('row-activated', self.on_row_activated)
  78.         self.set_model(quotes_updater)
  79.         
  80.     def _getcelldata_symbol(self, column, cell, model, iter):
  81.         cell.set_property('text', model[iter][model.SYMBOL])
  82.  
  83.     def _getcelldata_value(self, column, cell, model, iter):
  84.         cell.set_property('text', "%.5g" % model[iter][model.VALUE])
  85.  
  86.     def _getcelldata_variation(self, column, cell, model, iter):
  87.         color = GREEN
  88.         if model[iter][model.VARIATION_PCT] < 0: color = RED
  89.         change_pct = model[iter][model.VARIATION_PCT]
  90.         cell.set_property('markup',
  91.             "<span foreground='%s'>%+.2f%%</span>" %
  92.             (color, change_pct))
  93.  
  94.     def _getcelldata_balance(self, column, cell, model, iter):
  95.         is_ticker_only = model[iter][model.TICKER_ONLY]
  96.         color = GREEN
  97.         if model[iter][model.BALANCE] < 0: color = RED
  98.         if is_ticker_only:
  99.             cell.set_property('text', '')
  100.         else:
  101.                     cell.set_property('markup',
  102.             "<span foreground='%s'>%+.2f</span>" %
  103.             (color, model[iter][model.BALANCE]))
  104.  
  105.     def _getcelldata_balancepct(self, column, cell, model, iter):
  106.         is_ticker_only = model[iter][model.TICKER_ONLY]
  107.         color = GREEN
  108.         if model[iter][model.BALANCE] < 0: color = RED
  109.         if is_ticker_only:
  110.             cell.set_property('text', '')
  111.         else:
  112.                     cell.set_property('markup',
  113.             "<span foreground='%s'>%+.2f%%</span>" %
  114.             (color, model[iter][model.BALANCE_PCT]))
  115.  
  116.     def on_row_activated(self, treeview, path, view_column):
  117.         ticker = self.get_model()[self.get_model().get_iter(path)][0]
  118.         if ticker == None:
  119.             return
  120.  
  121.         invest.chart.show_chart([ticker])
  122.  
  123. #class InvestTicker(gtk.Label):
  124. #    def __init__(self):
  125. #        gtk.Label.__init__(self, _("Waiting..."))
  126. #        
  127. #        self.quotes = []
  128. #        gobject.timeout_add(TICKER_TIMEOUT, self.scroll_quotes)
  129. #        
  130. #        get_quotes_updater().connect('quotes-updated', self.on_quotes_update)
  131. #                        
  132. #    def on_quotes_update(self, updater):
  133. #        self.quotes = []
  134. #        updater.foreach(self.update_quote, None)
  135. #    
  136. #    def update_quote(self, model, path, iter, user_data):
  137. #        color = GREEN
  138. #        if model[iter][model.BALANCE] < 0:
  139. #            color = RED
  140. #        
  141. #        self.quotes.append(
  142. #            "%s: <span foreground='%s'>%+.2f (%+.2f%%)</span> %.2f" %
  143. #            (model[iter][model.SYMBOL], color, model[iter][model.BALANCE], model[iter][model.BALANCE_PCT], model[iter][model.VALUE]))
  144. #                
  145. #    def scroll_quotes(self):
  146. #        if len(self.quotes) == 0:
  147. #            return True
  148. #        
  149. #        q = self.quotes.pop()
  150. #        self.set_markup("<span face='Monospace'>%s</span>" % q)
  151. #        self.quotes.insert(0, q)
  152. #        
  153. #        return True
  154. #
  155. #gobject.type_register(InvestTicker)
  156.  
  157. class InvestTrend(gtk.Image):    
  158.     def __init__(self):
  159.         gtk.Image.__init__(self)
  160.         self.pixbuf = None
  161.         self.previous_allocation = (0,0)
  162.         self.connect('size-allocate', self.on_size_allocate)
  163.         get_quotes_updater().connect('quotes-updated', self.on_quotes_update)
  164.     
  165.     def on_size_allocate(self, widget, allocation):
  166.         if self.previous_allocation == (allocation.width, allocation.height):
  167.             return
  168.  
  169.         self.pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, allocation.height, allocation.height)
  170.         self.set_color("grey")
  171.         self.previous_allocation = (allocation.width, allocation.height)
  172.         
  173.     def set_color(self, color, opacity=0xFF):
  174.         if self.pixbuf != None:
  175.             try:
  176.                 color = pango.Color(color)
  177.                 factor = float(0xFF)/0xFFFF
  178.                 self.pixbuf.fill(
  179.                     int(color.red*factor)<<24|int(color.green*factor)<<16|int(color.blue*factor)<<8|opacity)
  180.                 self.set_from_pixbuf(self.pixbuf)
  181.             except Exception, e:
  182.                 print e
  183.  
  184.     def on_quotes_update(self, updater):
  185.         start_total = 0
  186.         now_total = 0
  187.         for row in updater:
  188.             # Don't count the ticker only symbols in the color-trend
  189.             if row[updater.TICKER_ONLY]:
  190.                 continue
  191.                 
  192.             var = row[updater.VARIATION]/100
  193.             now = row[updater.VALUE]
  194.  
  195.             start = now / (1 + var)
  196.             
  197.             portfolio_number = sum([purchase["amount"] for purchase in invest.STOCKS[row[updater.SYMBOL]]])
  198.             start_total += start * portfolio_number
  199.             now_total += now * portfolio_number
  200.         
  201.         day_var = 0
  202.         if start_total != 0:
  203.             day_var = (now_total - start_total) / start_total * 100
  204.  
  205.         color = int(2*day_var)
  206.         opacity = min(0xFF, int(abs(127.5*day_var)))
  207.         if day_var < 0:
  208.             color = COLORSCALE_NEGATIVE[min(len(COLORSCALE_NEGATIVE)-1, abs(color))]
  209.         else:
  210.             color = COLORSCALE_POSITIVE[min(len(COLORSCALE_POSITIVE)-1, abs(color))]
  211.  
  212.         self.set_color(color, opacity)
  213.     
  214. gobject.type_register(InvestTrend)
  215.